/* $RCSFile: DateDataType.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/DataTypes/DateDataType.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:29  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Globalization;
using System.Diagnostics;
using System.IO;
using System.ComponentModel;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;
using AddonNice.Diagnostics;

using Telerik.Web.UI;

namespace AddonNice.UI.DataTypes
{
	/// <summary>
	/// DateDataType
	/// </summary>
	public class DateDataType : StringDataType
	{
		public DateDataType()
		{
			InnerDataType = PropertiesDataType.Date;
		}

		public override string Val
		{
			get
			{
				return base.Val;
			}
			set
			{
				//Check type
				try
				{
				    DateTime.Parse(value,CultureInfo.InvariantCulture);
				}
				catch (Exception ex)
				{
                    if ( DataTypeTraceSwitch.Sw.TraceError )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]DateDataType Set Value: {0}, ex: {1}",value,ex,DataTypeTraceSwitch.Sw.Info));
                    throw;
				}
				base.Val = value;
			}
		}

		protected override void InitializeComponents()
		{
            if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]DateDataType InitializeComponents datatype description: {0}, UseRadControl: {1}, ReadOnly: {2}",
                    Description,_UsesRadControl,ReadOnly),DataTypeTraceSwitch.Sw.Info);
            if (!UseRadControl)
            {
                base.InitializeComponents();
                return;
            }
            // nb : culture defaults to thread UICulture
            RadDateInput tx         =   new RadDateInput();
            tx.Width                =   controlWidth;
            if (HandleChanges)
            {
                if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( "[V]DateDataType InitializeComponents HandleChanges set",DataTypeTraceSwitch.Sw.Info);
                tx.AutoPostBack     =   true;
                tx.TextChanged      +=  new EventHandler(tx_TextChanged);
            }
            innerControl            =   tx;
            if ( ReadOnly )
                tx.Enabled          =   false;
            _IsInitialized          =   true;
		}

        public override void DataBind()
        {
			try
			{
                if (!UseRadControl)
                    base.DataBind();
                else
                {
                     ((RadDateInput)innerControl).Text = Value2Save;
                }
                return;
			}
			catch (Exception ex)
			{
                if ( DataTypeTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]DateDataType DataBind ex: {0}",ex),DataTypeTraceSwitch.Sw.Info);
                throw;
			}
        }

        /// <summary>
        /// Update value from control: used by eventhandler after a control change event triggered
        /// </summary>
        public override  string ReadDataFromEditControl()
		{
            if (!UseRadControl)
                return base.ReadDataFromEditControl();
            Control Ctl =   EditControl;
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]DateDataType ReadDataFromEditControl BEG Control is null: {0}, Val: {1}.", (Ctl == null), Val ), DataTypeTraceSwitch.Sw.Info);
            if ( Ctl is RadDateInput)
            {
                RadDateInput Tx     =   (RadDateInput)Ctl;
                innerValue          =   Tx.Text;
             }
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]DateDataType ReadDataFromEditControl END InnerValue: {0}.", innerValue), DataTypeTraceSwitch.Sw.Info);
            return Val;
        }

        public override Control EditControl
		{
			get
			{
                if ( !UseRadControl)
                    return base.EditControl;
				if ( innerControl == null )
					InitializeComponents();
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]DateDataType EditControl Get Value2Save: {0}", Value2Save),DataTypeTraceSwitch.Sw.Info);
				return innerControl;
			}
			set
			{
                if (!UseRadControl)
                {
                    base.EditControl    =   value;
                    return;
                }
                if (value is RadDateInput)
                {
                    RadDateInput Tx    =    (RadDateInput)value;
                    innerControl       =    value;
                    if (HandleChanges)
                    {
                        Tx.AutoPostBack = true;
                        Tx.TextChanged += new EventHandler(tx_TextChanged);
                    }
                    //Update value from control
                    // DO NOT use Value but use InnerValue
                    innerValue = ((RadDateInput)innerControl).Text;
                    if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]DateDataType EditControl UseRadControl Set Value: {0}", innerValue),DataTypeTraceSwitch.Sw.Info);
                    return;
                }
				string msg  =   string.Format("[E]DateDataType Set UserRadControl EditControl: {0}, not RadDateInput",value.GetType());
				if ( GlobalTraceSwitch.Sw.TraceError )
                    Trace.WriteLine(msg,DataTypeTraceSwitch.Sw.Info);
				throw new ArgumentException(msg);
			}
		}

        public override string Description
		{
			get
			{
				return "DateTime";
			}
		}
	}

}